package com.bc.bczbweb.controller.Ajax;

import com.bc.bcapi.service.T_Erc_contractService;
import com.bc.bcapi.service.T_Sendcoin_logService;
import com.bc.bcapi.service.T_Wallet_infoService;
import com.bc.bcmodel.domain.T_User_walletModel;
import com.bc.bcmodel.enums.SendCoinEnum;
import com.bc.bcmodel.req.MngProductAuthReq;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import com.bc.bcapi.service.T_ProjectService;
import com.bc.bcmodel.domain.T_ProjectModel;
import com.bc.commonentity.enums.HttpReturnRnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.POST;
import java.math.BigInteger;

@RestController
@RequestMapping("ajax/t_project")
public class AjaxT_ProjectController {
    @Autowired
    T_ProjectService t_ProjectService;
    @Autowired
    T_Wallet_infoService t_wallet_infoService;
    @Autowired
    T_Erc_contractService t_erc_contractService;
    @Autowired
    T_Sendcoin_logService t_sendcoin_logService;

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public JsonResult update(MngProductAuthReq req) {
        JsonResult js = new JsonResult();
        if (StringUtils.isEmpty(req.getPid())) {
            return new JsonResult(HttpReturnRnums.Success);
        }
        T_ProjectModel t_projectModel = new T_ProjectModel();
        t_projectModel.setStar(req.getStar());
        t_projectModel.setPid(req.getPid());
        t_ProjectService.update(t_projectModel);
        return js;
    }

    @POST
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public JsonResult add(MngProductAuthReq req) {
        JsonResult js = new JsonResult();
        if (StringUtils.isEmpty(req.getPid())) {
            return new JsonResult(HttpReturnRnums.Success);
        }
        T_ProjectModel t_projectModel = new T_ProjectModel();
        t_projectModel.setPid(req.getPid());
        T_ProjectModel resultModel = t_ProjectService.findByNamedParam(t_projectModel);
        if (resultModel.getStatus() == -1 || resultModel.getStatus() == 1) {
            js.setMsg("当前状态无法审核");
            return js;
        }
        /**
         * 这里判断一下当前用户是否有ETH钱包，如果没有则创建
         * */
        if (req.getStatus() == 1) {
            //审核通过后创建ETH钱包
            t_wallet_infoService.writeWalletETH(req.getPid());

            try {
                //创建EOS钱包
//                t_wallet_infoService.writeWalletEos(req.getPid());
                //判断是否有合约地址，如果没有则创建
                t_erc_contractService.check(req);
                //代币奖励
                t_sendcoin_logService.setSendCoin(SendCoinEnum.PROJECTSUCCESS, resultModel.getUser_id(), req.getPid());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        int result = t_ProjectService.setAuth(req);
        return js;
    }

    @RequestMapping("del")
    public JsonResult del(BigInteger id) {
        JsonResult js = new JsonResult();
//        t_ProjectService.delete(id);
        return js;
    }
}